<template>
  <el-card class="middle-view">
    <template #header>
      <el-menu :default-active="state.activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
        <el-menu-item index="1">销售额</el-menu-item>
        <el-menu-item index="2">访问量</el-menu-item>
      </el-menu>

      <div class="right">
        <el-radio-group v-model="state.time1">
          <el-radio-button label="今日">今日</el-radio-button>
          <el-radio-button label="本周">本周</el-radio-button>
          <el-radio-button label="本月">本月</el-radio-button>
          <el-radio-button label="今年">今年</el-radio-button>
        </el-radio-group>
        <el-date-picker v-model="state.time2" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" size="default" :shortcuts="shortcuts" />
      </div>
    </template>
    <div class="content">
      <v-chart :option="getOption()" class="echarts"></v-chart>
      <div class="list-wrap">
        <div class="title">{{ state.activeIndex === "1" ? "门店销售额排名" : "门店访问量排名" }}</div>
        <div class="list">
          <!--  -->
          <div class="list-item" v-for="item in rankData" :key="item.no">
            <div class="no" :class="{ 'top-no': item.no <= 3 }">{{ item.no }}</div>
            <div class="name">{{ item.name }}</div>
            <div class="count">{{ item.count }}</div>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>
<script lang="ts">
export default {
  name: "MiddleView",
};
</script>
<script lang="ts" setup>
import { reactive, computed } from "vue";
import useFormat from "../../../hooks/useFormat";
import { useDataStore } from "@/stores/reportData";
const { numberFormat } = useFormat();
const dataStore = useDataStore();
const state = reactive({
  activeIndex: "1",
  time1: "本周",
  time2: "",
});
// 切换菜单的回调
const handleSelect = (val: string) => {
  state.activeIndex = val;
};
// 定于日期选择器需要的数组
const shortcuts = [
  {
    text: "最近一周",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      return [start, end];
    },
  },
  {
    text: "最近一个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      return [start, end];
    },
  },
  {
    text: "最近三个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
      return [start, end];
    },
  },
];

// 获取可视化需要的数据

// 销售额的横轴数据
const orderFullYearAxis = computed(() => dataStore.reportData.orderFullYearAxis || []);
// 销售额数据
const orderFullYear = computed(() => dataStore.reportData.orderFullYear || []);
// 访问量的横轴数据
const userFullYearAxis = computed(() => dataStore.reportData.userFullYearAxis || []);
// 访问量数据
const userFullYear = computed(() => dataStore.reportData.userFullYear || []);
//销售额排行榜数据
const orderRank = computed(() => dataStore.reportData.orderRank || []);
// 访问量排行榜数据
const userRank = computed(() => dataStore.reportData.userRank || []);

// 计算一个排行的数据
const rankData = computed(() => {
  return state.activeIndex === "1" ? orderRank.value : userRank.value;
});
const getOption = () => {
  const text = state.activeIndex === "1" ? "销售趋势" : "访问量趋势";
  return {
    // 标题
    title: {
      show: true,
      text,
      textStyle: {
        fontSize: "14",
      },
      left: "10",
    },
    // x轴
    xAxis: {
      data: state.activeIndex === "1" ? orderFullYearAxis.value : userFullYearAxis.value,
    },
    // y轴
    yAxis: {
      show: true,
      splitLine: {
        show: true,
        lineStyle: {
          color: "#333",
          type: "dotted",
          opacity: "0.5",
        },
      },
    },
    // 系列数据
    series: [
      {
        type: "bar",
        data: state.activeIndex === "1" ? orderFullYear.value : userFullYear.value,
        barWidth: "35%",
      },
    ],
    color: "skyblue",
    // 四周的位置
    grid: { left: 50, right: 50, top: 50, bottom: 50 },
    // 提示
    tooltip: {
      show: true,
      trigger: "axis",
    },
  };
};
</script>
<style lang="scss" scoped>
.middle-view {
  margin-top: 20px;
  :deep(.el-card__header) {
    border-bottom: none;
    position: relative;
    padding: 0 20px;
    .right {
      position: absolute;
      top: 0;
      height: 100%;
      right: 20px;
      display: flex;
      align-items: center;
      .el-date-editor {
        margin-left: 20px;
      }
    }
  }
  .content {
    display: flex;
    height: 270px;
    .echarts {
      flex: 5;
    }
    .list-wrap {
      flex: 2;
      .title {
        margin-top: 20px;
        font-size: 14px;
      }
      .list {
        margin-top: 15px;
        .list-item {
          display: flex;
          align-items: center;
          height: 20px;
          font-size: 12px;
          margin: 6px 20px 6px 0;
          .no {
            width: 20px;
            height: 20px;
            color: #333;
            text-align: center;
            line-height: 20px;
            &.top-no {
              border-radius: 50%;
              background-color: #000;
              color: #fff;
            }
          }
          .name {
            margin-left: 10px;
          }
          .count {
            flex: 1;
            text-align: right;
          }
        }
      }
    }
  }
}
</style>
